<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <j-dict-select-tag type="list" v-model="model.customerId"  dictCode="mws_t_arc_customers,code,id" placeholder="请选择户号" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <a-input v-model="model.cusName" placeholder="请输入户名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="用水性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="natureId">
              <j-dict-select-tag type="list" v-model="model.natureId"  dictCode="mws_t_base_natures,name,id" placeholder="请选择用水性质" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readMonth">
              <a-input v-model="model.readMonth" placeholder="请输入抄表月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="财务日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financialDate">
              <j-date placeholder="请选择财务日期" v-model="model.financialDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="财务月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financialMonth">
              <a-input v-model="model.financialMonth" placeholder="请输入财务月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="计费水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入计费水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="合计金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalMoney">
              <a-input-number v-model="model.totalMoney" placeholder="请输入合计金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="违约金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="penalty">
              <a-input-number v-model="model.penalty" placeholder="请输入违约金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="违约天数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="penaltyDay">
              <a-input-number v-model="model.penaltyDay" placeholder="请输入违约天数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="增值税金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vatMoney">
              <a-input-number v-model="model.vatMoney" placeholder="请输入增值税金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="杂项金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alienMoney">
              <a-input-number v-model="model.alienMoney" placeholder="请输入杂项金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="折扣金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discountMoney">
              <a-input-number v-model="model.discountMoney" placeholder="请输入折扣金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="折后金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入折后金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bookId">
              <j-dict-select-tag type="list" v-model="model.bookId"  dictCode="mws_t_mr_books,name,id" placeholder="请选择抄表本" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readerId">
              <j-select-user-by-dep v-model="model.readerId" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="冲账标志,标记该笔费用是冲账费用，某些地方不需要显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isStruck">
              <j-dict-select-tag type="list" v-model="model.isStruck"  dictCode="yn" placeholder="请选择冲账标志,标记该笔费用是冲账费用，某些地方不需要显示" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="作废标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isValid">
              <j-dict-select-tag type="list" v-model="model.isValid"  dictCode="yn" placeholder="请选择作废标志" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="用水价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priceId">
              <j-dict-select-tag type="list" v-model="model.priceId"  dictCode="" placeholder="请选择用水价格" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="计费明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="blSubExpensesTable.loading"
            :columns="blSubExpensesTable.columns"
            :dataSource="blSubExpensesTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BlExpensesForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        validatorRules: {
           customerId: [
              { required: true, message: '请输入户号!'},
           ],
           financialMonth: [
              { required: true, message: '请输入财务月份!'},
           ],
           amount: [
              { required: true, message: '请输入计费水量!'},
           ],
           totalMoney: [
              { required: true, message: '请输入合计金额!'},
           ],
           penalty: [
              { required: true, message: '请输入违约金!'},
           ],
           money: [
              { required: true, message: '请输入折后金额!'},
           ],
           isStruck: [
              { required: true, message: '请输入冲账标志,标记该笔费用是冲账费用，某些地方不需要显示!'},
           ],
           isValid: [
              { required: true, message: '请输入作废标志!'},
           ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['blSubExpenses', ],
        tableKeys:['blSubExpenses', ],
        activeKey: 'blSubExpenses',
        // 计费明细
        blSubExpensesTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '费用代码',
              key: 'itemCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '用水价格',
              key: 'priceId',
              type: FormTypes.select,
              dictCode:"mws_t_base_water_prices,price,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '用水性质',
              key: 'natureId',
              type: FormTypes.select,
              dictCode:"mws_t_base_natures,name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '阶梯号',
              key: 'tierNo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '水量',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '总金额',
              key: 'totalMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '增值税金额',
              key: 'vatMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '折扣金额',
              key: 'discountMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '作废标志',
              key: 'isValid',
              type: FormTypes.select,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/bl/blExpenses/add",
          edit: "/bl/blExpenses/edit",
          blSubExpenses: {
            list: '/bl/blExpenses/queryBlSubExpensesByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
     addBefore(){
            this.blSubExpensesTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.blSubExpenses.list, params, this.blSubExpensesTable)
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          blSubExpensesList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>